<?php

/**
 * 自动生成 SysRoleController
 * 
 * Apps/Backend/Controller/SysRoleController.class.php
 * Zix @ 2015-06-20
 */

namespace Backend\Controller;

use Common\Service\SysRoleService;
use Common\Service\SysDictionaryService;
use Common\Service\SysFuncService;
use Common\Service\SysRolePermissionService;

class SysRoleController extends BackendController {

  public function index() {
    $action = I('get.action', 'read');

    $data = $this->_init_data();
    $data['module_name'] = '系统角色';
    $data['module_url'] = U( $this->base_path . 'index');

    $SysRole = SysRoleService::instance();
    $data['config'] = array(
      'action' => $action,
      'action_label' => $this->action_label[$action],
      'menu_id' => $this->menu_id ,
      'module_url' => U($this->base_path . 'index'),
      'read_url'   => U($this->base_path . 'read' ),
      'create_url' => U($this->base_path . 'create'),
      'update_url' => U($this->base_path . 'update'),
      'delete_url' => U($this->base_path . 'delete'),
      'sys_type' => self::SYS_TYPE ,
      'get_permission_url' => U($this->base_path . 'get_permission'),
      'update_permission_url' => U($this->base_path . 'update_permission'),
      'page' => 1,
      'page_size' => C('PAGE_SIZE'),
      'row' => $SysRole->get_default_row() 
    );

    $data['param'] = array(
      'status' => array( 0 => '禁用' , 1 => '启用'),
      'rank' => $SysRole->rank ,
    );

    $this->display_with_layout($data, $this->controller .':index');
  }
  
  function read() {
    $post = I('post.');

    $config = array(
      'sys_type' => self::SYS_TYPE ,
      'rank' => $this->auth['rank'] ,
    );

    $SysRole = SysRoleService::instance();
    $data['rows'] = $SysRole->get_by_cond($config);
    $data['total'] = 1 ;
    $this->ajaxReturn($data);
  }

  function create() {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }
    
    $post['sys_type'] = 2 ;
    $SysRole = SysRoleService::instance();
    $ret = $SysRole->create($post);
    $this->ajaxReturn($ret);
  }

  function update($id = 0) {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }

    $SysRole = SysRoleService::instance();
    $ret = $SysRole->update($id, $post);
    $this->ajaxReturn($ret);
  }

  function delete() {
    $ids = I('post.ids');
    $SysRole = SysRoleService::instance();
    $ret = $SysRole->delete($ids); 
    $this->ajaxReturn($ret);
  }
  
  /**
   * 根据角色和系统类型 取权限
   */
  function get_permission() {
    $sys_type = I('post.sys_type');
    $role_id = I('post.role_id');

    //取角色操作权限
    $SysRolePermission = SysRolePermissionService::instance();
    $ret['p_data'] = $SysRolePermission->get_by_role_id($role_id);

    //取所有功能与操作
    $SysFunc = SysFuncService::instance();
    $data['func_data'] = $SysFunc->get_by_cond(array(
      'sys_type'   => $sys_type ,
      'status'     => 1 ,
      'opertation' => TRUE ,
    ));
    
    $this->assign($data);
    $ret['html'] = $this->fetch('permission');
    
    $this->ajaxReturn($ret);
  }
  
  /**
   * 更新角色权限
   */
  function update_permission() {
    $role_id = I('post.role_id'); 
    $opt_id = I('post.opt_id');
    
    $SysRolePermission = SysRolePermissionService::instance();
    $ret = $SysRolePermission->update_role_permission( $role_id , $opt_id );
    
    $this->ajaxReturn($ret);
  }
    
}
